package vision;

import geometrie.Point;
import geometrie.Transformation;

public class Camera {

	private Point refO;
	private Transformation projection = new Transformation();
	private Transformation tAnterieur;
	private boolean perspective;
	
	public Camera(int dimU, int dimV){		
		refO = new Point(new Integer(dimU).doubleValue()/2,new Integer(dimV).doubleValue()/2,0);
		makeParallele();
	}
	
	public Point getRef(){
		return refO;
	}
	public void setPoint(Point p){
		refO = new Point(p);
	}
	public Transformation getProjection(){
		return projection;
	}
	public void setProjection(Transformation t){
		if(isPerspective()){
			
			//Anulamos la transformación anterior
			//projection = Transformation.identite();
			//projection.getMatrice().setAt(3,3,0);
			//projection.getMatrice().setAt(3,2,1/f);
			if(tAnterieur!=null){
				projection = Transformation.compose(getProjection(), Transformation.inverse(tAnterieur.getMatrice()));
			}
			projection=Transformation.compose(getProjection(), t);
			tAnterieur=t;
			 
			/*
			if(tAnterieur == null)
				tAnterieur = Transformation.identite();
			
			//System.out.println("tAnt:\n " + tAnterieur);
			tAnterieur = Transformation.inverse(getProjection().getMatrice());
			
			
			//System.out.println("tAnt Despues: \n" + tAnterieur);
			projection = Transformation.compose(getProjection(), tAnterieur);
			
			System.out.println("Proj: \n" + getProjection());
			projection=Transformation.compose(getProjection(), t);
			System.out.println("Proj D: \n" + projection);
			//tAnterieur=t;
			 */
		}
		else
			projection=t;
	}
	public void makeParallele(){
		perspective = false;
		//Changer la Matrice du projection
		projection = Transformation.identite();
		projection.getMatrice().setAt(2,2,0);
		
		//Changer le point du camera
		refO.set(refO.getXCartesien(),refO.getYCartesien(),0);
		tAnterieur = null;
	}
	public void makePerspective(double f){
		perspective = true;
		//Changer la Matrice du projection
		projection = Transformation.identite();
		projection.getMatrice().setAt(3,3,0);

		projection.getMatrice().setAt(3,2,1/f);
		
		//Changer le point du camera
		refO.set(refO.getXCartesien(),refO.getYCartesien(),f);
		tAnterieur = null;
	}	
	
	public boolean isPerspective(){
		return perspective;
	}
}
